﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.Page
{
    public partial class cau_hinh_mail : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtMail;
        private DataTable dtMailDetail;
        private TMMAIL_CONFIG_BusinessLogic clsMailConfig = new TMMAIL_CONFIG_BusinessLogic();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGirdMail();
            }
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            lbThongBaoLoi.Text = "";
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        BindGirdMailDetail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#cauhinhmail");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                    }
                }
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            BindGirdMail();
        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTieuDeMail.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tiêu đề Mail !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtLinkActive1.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên link cấp 1 !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtLinkActive2.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên link cấp 2 !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtTaiKhoanMail.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tài khoản Mail !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtPasswords.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mật khẩu !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtTieuDeMail.Text != "" && txtLinkActive1.Text != "" 
            &&  txtLinkActive2.Text !="" && txtTaiKhoanMail.Text !=""
            &&  txtPasswords.Text !="")
            {
                UpdateMail();
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        //-------------list-view-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyCauHinhMail, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                lbThongBaoLoi.Text = "";
                if (e.CommandName == "Accept")
                {
                    if (Edit == false)
                    {
                        imgSave.Visible = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGirdMailDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#cauhinhmail");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                    }
                    else
                    {
                        imgSave.Visible = true;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGirdMailDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#cauhinhmail");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");


                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";
            }
        }
        //-----------pagger-----------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            BindGirdMail();
        }
        #endregion

        #region[Function]
        protected void BindGirdMail()
        {
            dtMail = clsMailConfig.BindGridMail();
            if (dtMail != null && dtMail.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtMail;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void BindGirdMailDetail_Edit(int id)
        {
            dtMailDetail = clsMailConfig.BindGridMailDetail_Edit(id);
            if (dtMailDetail != null && dtMailDetail.Rows.Count > 0)
            {
                lbID.Text = dtMailDetail.Rows[0]["IDMAIL"].ToString();
                txtTieuDeMail.Text = dtMailDetail.Rows[0]["TITILEMAIL"].ToString();
                txtLinkActive1.Text = dtMailDetail.Rows[0]["LINKACTIVE"].ToString();
                txtLinkActive2.Text = dtMailDetail.Rows[0]["LINKACTIVE1"].ToString();
                txtTaiKhoanMail.Text = dtMailDetail.Rows[0]["USERNAME"].ToString();
                txtPasswords.Text = dtMailDetail.Rows[0]["PASSWORDS"].ToString();
                txtNoiDungMail.Text = dtMailDetail.Rows[0]["CONTENTMAIL"].ToString();
            }
        }
        protected void UpdateMail()
        {
            try
            {
                VanTaiProperty.clsMAIL_CONFIGProperty obj = new clsMAIL_CONFIGProperty();
                obj.IDMAIL = int.Parse(lbID.Text);
                obj.TITILEMAIL = txtTieuDeMail.Text;
                obj.CONTENTMAIL = txtNoiDungMail.Text;
                obj.LINKACTIVE = txtLinkActive1.Text;
                obj.LINKACTIVE1 = txtLinkActive2.Text;
                obj.USERNAME = txtTaiKhoanMail.Text;
                obj.PASSWORDS = txtPasswords.Text;
                VanTaiBLL.TMMAIL_CONFIG_BusinessLogic.UpdateMailConfig(obj);
                lbThongBaoLoi.Text = "Cập nhật mẫu tin thành công !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
                BindGirdMail();
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Permisstion]
        protected void CheckPermisstion()
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                bool Is_Page_All = SecurityServices.HasPermisstionPage(StringContent.QuanLyCauHinhMail, int.Parse(cookie["RolesID"]), true);
                if (Is_Page_All == true)
                {
                    bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyCauHinhMail, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                    if (Edit == false)
                    {
                        cmdEdit.Visible = false;
                    }
                    else
                    {
                        cmdEdit.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("error-page.aspx");
                }
            }
        }
        #endregion
    }
}